<?php
class Payment
{
	const LIST_CREDIT_CARD = 'credit_card';
	const LIST_PAYMENT_METHOD = 'method';
	const LIST_PAYMENT_GATEWAY = 'gateway';

	const TOKEN_WRAPER = '%';

	/**
	 * calculates tax in amount
	 *
	 * @param int $tax
	 * @param float $amount
	 * @return float
	 */
    public static function getTax($tax, $amount)
	{
		return ($amount * $tax) / (100 + $tax);
	}

	/**
	 * configuration key 'app_payment' defined under this plugin app.yml
	 * 
	 * @param string $type
	 *		Payment::LIST_CREDIT_CARD,
	 *		Payment::LIST_PAYMENT_METHOD,
	 *		Payment::LIST_PAYMENT_GATEWAY
	 * @return array
	 */
	public static function getConfigurationItems($type)
	{
		return sfConfig::get('app_payment_'.$type);
	}

	/**
	 * feeds sfWidgetFormChoice
	 * 
	 * @param string $type
	 *		Payment::LIST_CREDIT_CARD,
	 *		Payment::LIST_PAYMENT_METHOD,
	 *		Payment::LIST_PAYMENT_GATEWAY
	 * @param bool $add_empty inserts empty key/value pair in choices list
	 * @return array
	 */
	public static function getConfigurationChoices($type, $add_empty = false)
	{
		$list = array();
		$add_empty && $list[] = '';
		foreach(self::getConfigurationItems($type) as $key => $item)
		{
			$list[$key] = $item['name'];
		}

		return $list;
	}

	/**
	 * parses template string and generates ProductOrder/Invoice id
	 * used by ProductOrder::create and Invoice::create factory methods
	 * 
	 * @param ProductOrder $obj
	 * @param Invoice $obj
	 * @param string $template ie. invoice_id_tpl: '%Y%%m%%d%-%cnt%'
	 * @return string generated id
	 */
	public static function generateId($obj, $template)
	{
		$content = $template;

		$tokens = array
		(
			'Y' => date("Y"),
			'm' => date("m"),
			'd' => date("d"),
			'cnt' => str_pad($obj->getNewCounterNum(), 6, "0", STR_PAD_LEFT)
		);
		
		foreach ($tokens as $key => $value)
		{
			$content = str_replace(Payment::TOKEN_WRAPER . $key . Payment::TOKEN_WRAPER, $value, $content, $count);
		}

		return $content;
	}
}
?>
